package com.mjf.jst.util;


import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

@Slf4j
@Component
public class RequestUtil {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 请求接口 -post请求 - Urlencoded格式参数
     *
     * @param url   url
     * @param param param
     * @param name  name
     * @return Map
     */
    public String reqForRespByUrlencoded(String url, MultiValueMap<String, String> param, String name) {
        String reqForResp;
        try {
            log.debug("Rest:" + name + "_Begin:请求uri:" + url + ",请求参数：" + param.toString());
            // REST HTTP HEADER
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            //rest http entity
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, httpHeaders);
            reqForResp = restTemplate.postForObject(url, request, String.class);
        } catch (Exception e) {
            log.error(name + "请求接口失败", e);
            throw new RuntimeException(e.getMessage());
        }
        return reqForResp;
    }

    /**
     * 请求接口 -post请求- json格式参数
     *
     * @param url   url
     * @param param param
     * @param name  name
     * @return Map
     */
    public String reqForResp(String url, Map<String, Object> param, String name) {
        String reqForResp;
        try {
            log.debug("Rest:" + name + "_Begin:请求uri:" + url + ",请求参数：{}", param);
            // REST HTTP HEADER
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            //rest http entity
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, httpHeaders);
            reqForResp = restTemplate.postForObject(url, request, String.class);
        } catch (Exception e) {
            log.error(name + "请求接口失败", e);
            throw new RuntimeException(e.getMessage());
        }
        return reqForResp;
    }

    /**
     * get请求-携带参数
     *
     * @param url   url
     * @param param param
     * @param name  name
     * @return Map
     */
    public String reqForGetWithParam(String url, MultiValueMap<String, String> param, String name) {
        String reqForResp;
        try {
            log.debug("Rest:" + name + "_Begin:请求uri:" + url + ",请求参数：" + JSON.toJSONString(param));
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(param);
            String ussss = builder.toUriString();
            reqForResp = restTemplate.getForObject(ussss, String.class);
        } catch (Exception e) {
            log.error(name + "请求接口失败", e);
            throw new RuntimeException(e.getMessage());
        }
        return reqForResp;
    }

    /**
     * get请求-不携带参数
     *
     * @param url  url
     * @param name name
     * @return Map
     */
    public String reqForGetWithOutParam(String url, String name) {
        String reqForResp;
        try {
            log.debug("Rest:" + name + "_Begin:请求uri:" + url);
            reqForResp = restTemplate.getForObject(url, String.class);
        } catch (Exception e) {
            log.error(name + "请求接口失败", e);
            throw new RuntimeException(e.getMessage());
        }
        return reqForResp;
    }
}
